#include <all_wlib.h>
#pragma hdrstop
//#pragma package(smart_init)
#pragma link "bklist"
#pragma link "hcomps"
#pragma link "BkList"
#pragma resource "*.dfm"

#include "nv_Names.h"
//---------------------------------------------------------------------------
void EditNames( NameArray& al )
  {  HAutoPtr<TFrmNamesList> d( new TFrmNamesList(al) );

     d->ShowModal();
}

//---------------------------------------------------------------------------
__fastcall TFrmNamesList::TFrmNamesList( NameArray& al )
  : TForm( (TComponent*)NULL ),
    Aliases( al )
  {
}
//---------------------------------------------------------------------------
void __fastcall TFrmNamesList::edtAddrChange(TObject *Sender)
  {  bool is = list->ItemIndex >= 0 && edtAddr->Text.Length() && edtName->Text.Length();
     btnChange->Enabled = is;
     btnChange->Enabled = is;
}
//---------------------------------------------------------------------------
void __fastcall TFrmNamesList::listClick(TObject *Sender)
  {  PNameInfo p = Aliases[ list->ItemIndex ];

     edtAddr->Text = p?p->Addr.Text():"";
     edtName->Text = p?p->Name.Text():"";
     edtAddrChange(NULL);
}
//---------------------------------------------------------------------------
bool __fastcall TFrmNamesList::listDrawItemSection(TObject *Sender,
      TCanvas *dc, int Index, THeaderSection *Section, const MyRect &Rect,
      const TOwnerDrawState &State)
  {  PNameInfo p = Aliases.Item(Index);
     if (!p) return false;

     CONSTSTR m = NULL;

     switch( Section->Index ) {
       case 0: m = p->Addr.Text(); break;
       case 1: m = p->Name.Text(); break;
     }
     Assert( m );
     TC_TextOut( dc,Rect.Inflated(-5,0),m,DT_SINGLELINE|DT_VCENTER|DT_LEFT );
 return true;
}
//---------------------------------------------------------------------------
void __fastcall TFrmNamesList::btnAddClick(TObject *Sender)
  {  NameInfo nm( edtAddr->Text.c_str(),"" );

    if ( Aliases.FindAddr(nm) == -1 ) {
      Aliases.Add( NameInfo(edtAddr->Text.c_str(),edtName->Text.c_str()) );
      Aliases.SortAddr();
      list->ItemsCount = Aliases.Count();
      list->Invalidate();
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrmNamesList::btnDelClick(TObject *Sender)
  {
    Aliases.DeleteNum( list->ItemIndex );
    Aliases.SortAddr();
    list->ItemsCount = Aliases.Count();
    list->Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TFrmNamesList::btnChangeClick(TObject *Sender)
  {  PNameInfo p = Aliases.Item( list->ItemIndex );
     if (p) {
       p->Addr = edtAddr->Text.c_str();
       p->Name = edtName->Text.c_str();
       Aliases.SortAddr();
       list->Invalidate();
     }
}
//---------------------------------------------------------------------------
void __fastcall TFrmNamesList::FormShow(TObject *Sender)
  {
     list->ItemsCount = Aliases.Count();
     list->Invalidate();
     edtAddr->Text = "";
     edtName->Text = "";
}
//---------------------------------------------------------------------------
